package com.xdu.spm.Dao.Interface;

import com.xdu.spm.Dao.Entity.Librarian;
import com.xdu.spm.Dao.Entity.Reader;
import com.xdu.spm.Dao.Mapper.LibrarianMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class LibrarianDao implements LibrarianInterface{
    InputStream is = null;
    SqlSessionFactory factory = null;
    SqlSession session = null;
    LibrarianMapper mapper = null;

    public void init() throws IOException {
        is = Resources.getResourceAsStream("com/xdu/spm/Dao/config.xml");
        factory = new SqlSessionFactoryBuilder().build(is);
        session = factory.openSession();
        mapper = session.getMapper(LibrarianMapper.class);
    }

    public void close() throws IOException {
        session.commit();
        session.close();
        is.close();
    }

    public Librarian findLibByAccount(String Account) throws IOException {
        init();
        Librarian lib = mapper.findLibByAccount(Account);
        close();
        return lib;
    }

    public List<Reader> findAllReader() throws IOException {
        init();
        List<Reader> readers = mapper.findAllReader();
        close();
        return readers;
    }
}
